package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityMoreMovieBinding;
import flc.ast.fragment.TabFragment;
import stark.common.basic.base.BaseWebviewActivity;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.view.container.StkLinearLayout;
import stark.common.bean.StkResBean;

public class MoreMovieActivity extends BaseAc<ActivityMoreMovieBinding> {

    //电影推荐
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabTitle = new String[]{};
    private String[] tabUrl=new String[]{};

    @Override
    protected int onCreate() {
        return R.layout.activity_more_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);
        mDataBinding.ivMoreMovieBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        tabTitle = new String[]{getString(R.string.video_tab_title1), getString(R.string.video_tab_title2), getString(R.string.video_tab_title3), getString(R.string.video_tab_title4), getString(R.string.video_tab_title5)};
        tabUrl = new String[]{AppConfig.MOVIE_DATA1,AppConfig.MOVIE_DATA2,AppConfig.MOVIE_DATA3,AppConfig.MOVIE_DATA4,AppConfig.MOVIE_DATA5};
    }

    @Override
    protected void initData() {
        for (int i = 0; i < tabTitle.length; i++) {
            fragments.add(new TabFragment().newInstance(tabUrl[i], i));
            mDataBinding.tlVideo.addTab(mDataBinding.tlVideo.newTab().setText(tabTitle[i]));
        }
        mDataBinding.vpVideo.setAdapter(new ArticleAdapter(getSupportFragmentManager()));
        mDataBinding.tlVideo.setupWithViewPager(mDataBinding.vpVideo);
        //给tab每个item设置值
        for (int i = 0; i < mDataBinding.tlVideo.getTabCount(); i++) {
            TabLayout.Tab tab = mDataBinding.tlVideo.getTabAt(i);
            if (tab != null) {
                tab.setCustomView(getTabView(i));
            }
        }
        // 设置默认第一个被选中
        View view = mDataBinding.tlVideo.getTabAt(0).getCustomView();
        TextView tv = view.findViewById(R.id.tvCategoryItemName);
        tv.setTextColor(Color.parseColor("#FF000000"));
        StkLinearLayout llCategoryItem=view.findViewById(R.id.llCategoryItem);
        llCategoryItem.setBackgroundResource(R.drawable.shape_category);
        //设置TabLayout点击事件
        mDataBinding.tlVideo.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvCategoryItemName);
                tv.setTextColor(Color.parseColor("#FF000000"));
                StkLinearLayout llCategoryItem=view.findViewById(R.id.llCategoryItem);
                llCategoryItem.setBackgroundResource(R.drawable.shape_category);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                TextView tv = view.findViewById(R.id.tvCategoryItemName);
                tv.setTextColor(Color.parseColor("#FF949494"));
                StkLinearLayout llCategoryItem=view.findViewById(R.id.llCategoryItem);
                llCategoryItem.setBackgroundResource(R.drawable.shape_category_off);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private class ArticleAdapter extends FragmentStatePagerAdapter {
        public ArticleAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        //此方法用来显示tab上的名字
        @Override
        public CharSequence getPageTitle(int position) {
            return tabTitle[position];
        }
    }

    //自定义Tab布局
    private View getTabView(int i) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_category, null);
        TextView text = view.findViewById(R.id.tvCategoryItemName);
        text.setText(tabTitle[i]);
        text.setTextColor(Color.parseColor("#FF949494"));
        StkLinearLayout llCategoryItem=view.findViewById(R.id.llCategoryItem);
        llCategoryItem.setBackgroundResource(R.drawable.shape_category_off);
        llCategoryItem.setPadding(42,20,42,20);
        return view;
    }

}